/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
 * Description: the log module for demuser
 * Author: HiMobileCam middleware develop team
 * Create: 2017-12-22
 */
#include "ffdemux_log.h"

#include "securec.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

#define MAX_PRINT_STR_LEN  1024

static HiFfDemuxLogLevel g_ffDemuxLogLevel = HI_FFDEMUX_LOG_LEVEL_ERR;
static HiFfdemuxOutputFunc g_ffdemuxOutputFunc = HI_NULL;

HI_S32 FfdemuxSetEnabledLevel(HiFfDemuxLogLevel level)
{
    g_ffDemuxLogLevel = level;
    return HI_SUCCESS;
}

HI_S32 FfdemuxSetOutputFunc(HiFfdemuxOutputFunc func)
{
    g_ffdemuxOutputFunc = func;
    return HI_SUCCESS;
}

#ifdef ENABLE_LOG
HI_VOID FFDEMUX_PRINTF(const HI_CHAR modName[], HiFfDemuxLogLevel level, const HI_CHAR fmt[], ...)
{
    HI_CHAR printStr[MAX_PRINT_STR_LEN] = { 0 };

    if (level < g_ffDemuxLogLevel) {
        return;
    }

    va_list args;
    if (!g_ffdemuxOutputFunc) {
        va_start(args, fmt);
        if (vsnprintf_s(printStr, MAX_PRINT_STR_LEN, MAX_PRINT_STR_LEN - 1, fmt, args) < 0) {
            va_end(args);
            return;
        }
        va_end(args);
        printf("[%s] %s", modName, printStr);
    } else {
        g_ffdemuxOutputFunc("[%s] ", modName);
        va_start(args, fmt);
        g_ffdemuxOutputFunc(fmt, args);
        va_end(args);
    }

    return;
}
#endif

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
